package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/*
 *@Create Date:  2019/11/20    20:06
 *
 *@Description:
 */

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @RequestMapping("/findMenu")
    public Result findMenu() {
        try {
            //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            List<Menu> menuList = new ArrayList<>();
            Set<Integer> menuIds = new HashSet<>();
            if (user != null) {
                String username = user.getUsername();
                int userId = menuService.findUserId(username);
                List<Integer> roleIds = menuService.findRoleId(userId);
                for (Integer roleId : roleIds) {
                    Set<Integer> set = menuService.findMenuId(roleId);
                    for (Integer integer : set) {
                        menuIds.add(integer);
                    }
                }

                Menu menu = new Menu();
                for (Integer menuId : menuIds) {
                    menu = menuService.findById(menuId);
                    if (menu.getLevel() == 1) {
                        List<Menu> list = menuService.findByParentMenuId(menu.getId(), menuIds);
                        menu.setChildren(list);
                        menuList.add(menu);
                    }
                }
            }
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu, Integer[] menuIds) {
        try {
            menuService.edit(menu, menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);//新增失败
        }
        return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);//新增成功
    }

    //根据ID查询检查组
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Menu checkGroup = menuService.findById(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, checkGroup);//查询成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);//查询失败
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = menuService.pageQuery(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/add")
    public Result add(Integer[] menuIds, @RequestBody Menu menu) {
        try {
            menuService.add(menu, menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
        return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Menu> list = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, list);//查询成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);//查询失败
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            menuService.delete(id);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }

    @RequestMapping("/findAssociation")
    public Result findAssociation(Integer id) {
        try {
            List<Integer> list = menuService.findAssociation(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
}
